/**
 * 类名：DepartmentPaserHandler
 * 创建日期： 2014-10-22 下午4:20:02
 * 
 */
package com.erms.domain.handler;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.erms.bean.Department;
import com.erms.util.LogUtil;
import com.erms.util.http.XmlParserHandler;

/**
 * @author hmb
 * 备注： 
 *
 */
public class DepartmentPaserHandler implements XmlParserHandler<Department> {
	private Logger logger = LogUtil.getLogger(getClass());
	
	/**
	 * @see com.erms.util.http.XmlParserHandler#onParse(org.jsoup.nodes.Document)
	 * @param document
	 * @return
	 */
	@Override
	public List<Department> onParse(Document document) {

		List<Department> departmentList = new ArrayList<>();
		
		Elements departmentElements = document.select("option");
		int n = departmentElements.size();
		//long oldTime = System.currentTimeMillis();
		
		for (int i = 0; i < n; i++) {
			Element departmentElement = departmentElements.get(i);
			Department department = new Department();
			Integer id = Integer.valueOf(departmentElement.attr("value"));
			String name = departmentElement.text().trim();
			department.setId(id);
			department.setName(name);
			
			departmentList.add(department);
			logger.debug(id.toString());
			logger.debug(name);
			
		}
		//logger.debug("解析和持久化共计用时(ms)："+(System.currentTimeMillis() - oldTime));
		return departmentList;
	}

	/**
	 * @see com.erms.util.http.XmlParserHandler#onError()
	 */
	@Override
	public void onError() {

		// TODO 自动生成的方法存根
		
	}

}
